package com.atguigu.system.controller;

import com.atguigu.common.result.Result;
import com.atguigu.model.system.SysRole;
import com.atguigu.model.vo.role.*;
import com.atguigu.system.annotation.Log;
import com.atguigu.system.enums.BusinessType;
import com.atguigu.system.service.SysRoleServiceImpl;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * SysRoleController
 * </p>
 *
 * @author flyduck
 * @since 2023/10/23
 */
@Api(tags = "角色管理接口")
@RestController
@RequestMapping("/admin/system/sysRole")
public class SysRoleController {

    @Autowired
    private SysRoleServiceImpl sysRoleServiceImpl;

    @ApiOperation("给角色分配菜单权限")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestBody SysRoleAssignMenuReqVo reqVo){
        sysRoleServiceImpl.doAssign(reqVo);
        return Result.ok();
    }

    @ApiOperation("根据角色获取菜单权限")
    @GetMapping("/toAssign/{roleId}")
    public Result<List<SysRoleAssignMenuQueryRespVo>> toAssign(@PathVariable("roleId") Long roleId){
        List<SysRoleAssignMenuQueryRespVo> list = sysRoleServiceImpl.findMenuListByRoleId(roleId);
        return Result.ok(list);
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("批量删除")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> ids){
        sysRoleServiceImpl.batchRemove(ids);
        return Result.ok();
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.update')")
    @ApiOperation("修改角色")
    @PostMapping("/update")
    public Result<Void> updateRole(@RequestBody SysRoleUpdateReqVo sysRoleSaveReqVo){
        boolean isSuccess = sysRoleServiceImpl.updateRole(sysRoleSaveReqVo);
        if (isSuccess) {
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("根据id查询")
    @PostMapping("/findRoleById/{id}")
    public Result<SysRoleQueryRespVo> findRoleById(@PathVariable("id") Long id){
        SysRoleQueryRespVo sysRoleQueryRespVo = sysRoleServiceImpl.findRoleById(id);
        return Result.ok(sysRoleQueryRespVo);
    }

    @Log(title = "角色管理",businessType = BusinessType.INSERT)
    @PreAuthorize("hasAuthority('bnt.sysRole.add')")
    @ApiOperation("添加角色")
    @PostMapping("/save")
    public Result<Void> saveRole(@RequestBody SysRoleSaveReqVo sysRoleSaveReqVo){
        boolean isSuccess = sysRoleServiceImpl.save(sysRoleSaveReqVo);
        if (isSuccess) {
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("条件分页查询")
    @GetMapping("/{page}/{limit}")
    public Result<IPage<SysRoleQueryRespVo>> findPageQueryRole(@PathVariable("page") Long page,
                                                               @PathVariable("limit") Long limit,
                                                               SysRoleQueryReqVo sysRoleQueryReqVo){
        Page<SysRole> pageParam = new Page<>(page, limit);
        IPage<SysRoleQueryRespVo> pageModel = sysRoleServiceImpl.selectPage(pageParam, sysRoleQueryReqVo);
        return Result.ok(pageModel);
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.remove')")
    @ApiOperation("根据id逻辑删除角色")
    @DeleteMapping("/remove/{id}")
    public Result<Void> removeRole(@PathVariable("id") Long id){
        boolean isSuccess = sysRoleServiceImpl.removeById(id);
        if (isSuccess) {
            return Result.ok();
        }else {
            return Result.fail();
        }
    }

    @PreAuthorize("hasAuthority('bnt.sysRole.list')")
    @ApiOperation("查询所有角色")
    @GetMapping("/findAll")
    public Result<List<SysRoleQueryRespVo>> findAllRole(){
        List<SysRoleQueryRespVo> list = sysRoleServiceImpl.findAllRole();
        return Result.ok(list);
    }
}
